home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Processes / quitapps / quitapps.c
Encoding:
C/C++ Source or Header  |  1992-07-15  |  2.4 KB  |  109 lines  |  [TEXT/KAHL]

  1. /**********************************
  2.  
  3.     QuitApps
  4.     DTS Code Snippet to quit all running applications (except yourself)
  5.     
  6.     note: to work properly, the calling application must have a standard
  7.     event loop with menus (to support puppet string quits for apps that
  8.     don't support core appleevents.
  9.     
  10.     note#2: remember to set the applevent aware flag in your app if you use
  11.     this code
  12.     
  13.     written by Steven Falkenburg 9/4/91
  14.  
  15. **********************************/
  16.  
  17. #include <Processes.h>
  18. #include <AppleEvents.h>
  19.  
  20.  
  21. /* protos */
  22. void QuitAllApps(void);
  23. OSErr QuitAnApp(ProcessSerialNumber *proc);
  24. short GetNumProcs(void);
  25.  
  26.  
  27. /* this is the entry procedure.  call this to start things going */
  28.  
  29. void QuitAllApps(void)
  30. {
  31.     ProcessSerialNumber ourProc,curProc,*quitApps;
  32.     OSErr err;
  33.     Boolean same;
  34.     short numApps,curAppIndex;
  35.     
  36.     numApps = GetNumProcs() - 1; /* don't count ourselves */
  37.     quitApps = (ProcessSerialNumber *)NewPtr(numApps*sizeof(ProcessSerialNumber));
  38.     if (MemError()!=noErr)
  39.         return;
  40.         
  41.     err = GetCurrentProcess(&ourProc);
  42.     if (err!=noErr)
  43.         return;
  44.     
  45.     curProc.highLongOfPSN = 0;
  46.     curProc.lowLongOfPSN = kNoProcess;
  47.     curAppIndex = 0;
  48.     
  49.     while (GetNextProcess(&curProc)==noErr && curAppIndex<numApps) {
  50.         err = SameProcess(&ourProc,&curProc,&same);
  51.         if (err==noErr && !same) {
  52.             BlockMove(&curProc,&quitApps[curAppIndex++],sizeof(ProcessSerialNumber));
  53.         }
  54.     }
  55.     
  56.     for (curAppIndex=0; curAppIndex<numApps; curAppIndex++)
  57.         QuitAnApp(&quitApps[curAppIndex]);
  58.     
  59.     DisposPtr((Ptr)quitApps);
  60. }
  61.  
  62.  
  63. /* gets the number of current processes */
  64.  
  65. short GetNumProcs(void)
  66. {
  67.     ProcessSerialNumber curProc;
  68.     short numProcs;
  69.     
  70.     numProcs = 0;
  71.     curProc.highLongOfPSN = 0;
  72.     curProc.lowLongOfPSN = kNoProcess;
  73.     
  74.     while (GetNextProcess(&curProc)==noErr)
  75.         numProcs++;
  76.     
  77.     return numProcs;
  78. }
  79.  
  80.  
  81. /* quits an app of the given process id */
  82.  
  83. OSErr QuitAnApp(ProcessSerialNumber *proc)
  84. {
  85.     OSErr err;
  86.     AEAddressDesc target;
  87.     AppleEvent theAE,aeReply;
  88.     
  89.     theAE.dataHandle = aeReply.dataHandle = target.dataHandle = nil;
  90.     
  91.     err = AECreateDesc(typeProcessSerialNumber,(Ptr)proc,sizeof(ProcessSerialNumber),&target);
  92.     if (err!=noErr)
  93.         return err;
  94.  
  95.     err = AECreateAppleEvent(kCoreEventClass,kAEQuitApplication,&target,
  96.                     kAutoGenerateReturnID,kAnyTransactionID,&theAE);
  97.     if (err!=noErr) {
  98.         AEDisposeDesc(&target);
  99.         return err;
  100.     }
  101.      
  102.     err = AESend(&theAE,&aeReply,kAENoReply,kAENormalPriority,kNoTimeOut,nil,nil);
  103.         
  104.     AEDisposeDesc(&target);
  105.     AEDisposeDesc(&theAE);
  106.     
  107.     return err;
  108. }
  109.